package com.sunstar.koobaselib.kootl.rvItemtouch.ui;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import com.sunstar.koobaselib.R;
import com.sunstar.koobaselib.kootl.comm.base.BaseActivity;
import com.sunstar.koobaselib.kootl.comm.config.CommFinalData;
import com.sunstar.koobaselib.kootl.comm.helper.CacheHelper;
import com.sunstar.koobaselib.kootl.comm.rvheaderfooter.RVHeaderFooterAdapter;
import com.sunstar.koobaselib.kootl.comm.tabhost.TabHostShowDataInfo;
import com.sunstar.koobaselib.kootl.rvItemtouch.adapter.ItemTouchHelperRVAdapter;
import com.sunstar.koobaselib.kootl.rvItemtouch.bean.ItemTouchBean;
import com.sunstar.koobaselib.kootl.rvItemtouch.contract.ItemTouchContract;
import com.sunstar.koobaselib.kootl.rvItemtouch.presenter.ItemTouchPresenterImpl;

import java.util.ArrayList;
import java.util.List;

import static com.sunstar.koobaselib.R.id.id_recycler_view;

public class ItemTouchActivity extends BaseActivity<ItemTouchContract.Presenter> implements ItemTouchContract.View<List<ItemTouchBean>> {
    @Override
    protected int setupLayoutId() {
        return R.layout.activity_item_touch;
    }

    @Override
    protected int setupToolbarResId() {
        return R.id.id_toolbar;
    }

    @Override
    protected ItemTouchContract.Presenter setupPresenter() {
        return new ItemTouchPresenterImpl(this);
    }

    @Override
    protected TabHostShowDataInfo setupTabHostDataInfo() {
        return null;
    }

    @Override
    protected boolean notNeedSwipeBack() {
        return false;
    }

    @Override
    protected boolean setupToolbarTitleCenter() {
        return true;
    }

    @Override
    protected boolean setupHideHomeAsUp() {
        return false;
    }

    @Override
    protected int setupCollapsingToolbarLayoutResId() {
        return 0;
    }

    @Override
    protected int setupSwipeRefreshLayoutResId() {
        return 0;
    }

    @Override
    protected int setupRecyclerViewResId() {
        return 0;
    }

    @Override
    protected RVHeaderFooterAdapter setupRVHeaderFooterAdapter() {
        return null;
    }

    @Override
    protected void toLoadMoreData() {

    }

    @Override
    protected int setupMenuResId() {
        return R.menu.menu_finish;
    }

    @Override
    protected int setupStatusBarColorResId() {
        return 0;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int i1 = item.getItemId();
        if (i1 == R.id.id_menu_item_ok) {
            StringBuffer sb = new StringBuffer("订阅了：");
            for (int i = 0; i < mItemTouchHelperRVAdapter.getItemTouchBeanList().size(); i++) {
                if (mItemTouchHelperRVAdapter.getItemTouchBeanList().get(i).getViewType()
                        == ItemTouchHelperRVAdapter.VIEW_TYPE_MORE_TITLE) {
                    break;
                }
                sb.append(mItemTouchHelperRVAdapter.getItemTouchBeanList().get(i).getText());
                sb.append(",");
            }

            /**
             * 缓存
             */
            //ArrayList<ItemTouchBean> itemTouchBeanArrayList = new ArrayList<>();
            //itemTouchBeanArrayList.addAll(mItemTouchHelperRVAdapter.getItemTouchBeanList());
            CacheHelper.putDataList(CommFinalData.CACHE_NEWS_CHANNEL, mItemTouchHelperRVAdapter.getItemTouchBeanList());

            //
            // showMessageDialog(sb.toString());

            //showSnack(sb.toString());

            finishAtyAfterTransition();


        }
        return super.onOptionsItemSelected(item);
    }

    ItemTouchHelperRVAdapter mItemTouchHelperRVAdapter;
    RecyclerView mRecyclerView;
    List<ItemTouchBean> mItemTouchBeanList = new ArrayList<>();

    @Override
    protected void begin() {
        //
        setToolbarTitle(R.string.nav_title_channel);



        mRecyclerView = (RecyclerView) findViewById(id_recycler_view);
        // id_recycler_view.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        // id_recycler_view.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());

        mItemTouchHelperRVAdapter = new ItemTouchHelperRVAdapter(mItemTouchBeanList,
                mRecyclerView, R.layout.item_grid_normal);

        mRecyclerView.setAdapter(mItemTouchHelperRVAdapter);


        mPresenter.gainCountData(Integer.MAX_VALUE);

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showNoDataNotice() {

    }

    @Override
    public void hideNoDataNotice() {

    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void setupData(List<ItemTouchBean> data) {
        mItemTouchBeanList.clear();
        mItemTouchBeanList.addAll(data);
        mItemTouchHelperRVAdapter.notifyDataSetChanged();
    }
}
